//
//  MainViewController.m
//  News
//
//  Created by Han on 15-4-13.
//  Copyright (c) 2015年 IPingLun. All rights reserved.
//

#import "MainViewController.h"

#define NEWS_COLLECTION_VIEW_TAG 100

@interface MainViewController () {
    UIView *mIndicatorView;
    UIScrollView *mNewsScrollView;
    NewsCollectionView *mCurrentCollectionView;
    UIButton *mChannelButton0;
    UIButton *mChannelButton1;
    
    UIButton *mShadeButton;
    MenuView *mRightMenu;
    
    int mScrollViewPage;
}

@end

@implementation MainViewController

+ (MainViewController *)sharedInstance
{
    static dispatch_once_t pred;
    static MainViewController *controller;
    dispatch_once(&pred, ^{
        controller = [[self alloc] init];
    });
    return controller;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self drawView];
    
    [self fristLoadData];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void) fristLoadData
{
    [mCurrentCollectionView fristLoadData];
}
 
- (void)jumpToDetail:(int)newsId {

    
    NewsDetailViewController *newsDetail = NewsDetailViewController.new;
    newsDetail.newsId = newsId;

    [self.navigationController pushViewController:newsDetail animated:YES];


}


- (void)drawView {
    
    self.view.backgroundColor = BG_COLOR;
    
    UIView *navigationBar = UIView.new;
    navigationBar.backgroundColor = MAIN_COLOR;
    [self.view addSubview:navigationBar];
    
    UIView *parentView = self.view;
    
    [navigationBar makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(parentView.top).offset(0);
        make.left.equalTo(parentView.left).offset(0);
        make.right.equalTo(parentView.right).offset(0);
        make.height.equalTo(54);
    }];
    
    UILabel *titleLabel = UILabel.new;
    titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
    titleLabel.text = @"爱评论";
    titleLabel.textColor = [UIColor whiteColor];
    [navigationBar addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navigationBar);
        make.top.equalTo(navigationBar.top).offset(20);
        make.bottom.equalTo(navigationBar.bottom).offset(0);
    }];
    
    
    UIButton *rightButton = UIButton.new;
    rightButton.exclusiveTouch = YES;
    [rightButton addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    [navigationBar addSubview:rightButton];
    
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navigationBar.top).offset(20);
        make.right.equalTo(parentView.right).offset(0);
        make.size.equalTo(CGSizeMake(44, 34));
    }];
    
    UIImageView *imageView = UIImageView.new;
    imageView.image = [UIImage imageNamed:@"ic_menu_light"];
    [rightButton addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(rightButton);
        make.size.equalTo(CGSizeMake(24, 24));
    }];
    
    mChannelButton0 = UIButton.new;
    mChannelButton0.tag = 0;
    mChannelButton0.backgroundColor = NAV_COLOR;
    [mChannelButton0 setTitle:@"新闻" forState:UIControlStateNormal];
    mChannelButton0.titleLabel.font = [UIFont systemFontOfSize: 12.5];
    [mChannelButton0 setTitleColor:UIColorFromRGB(0xAAAAAA) forState:UIControlStateNormal];
    [mChannelButton0 setTitleColor:MAIN_COLOR forState:UIControlStateSelected];
    [mChannelButton0 addTarget:self action:@selector(clickChannelButton:) forControlEvents:UIControlEventTouchUpInside];
    mChannelButton0.selected = YES;
    [self.view addSubview:mChannelButton0];
    
    [mChannelButton0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navigationBar.bottom).offset(0);
        make.left.equalTo(parentView.left).offset(0);
        make.size.equalTo(CGSizeMake(MAIN_SCREEN_WIDTH/2, 28));
    }];
    
    
    mChannelButton1 = UIButton.new;
    mChannelButton1.tag = 1;
    mChannelButton1.backgroundColor = NAV_COLOR;
    [mChannelButton1 setTitle:@"阅读" forState:UIControlStateNormal];
    mChannelButton1.titleLabel.font = [UIFont systemFontOfSize: 12.5];
    [mChannelButton1 setTitleColor:UIColorFromRGB(0xAAAAAA) forState:UIControlStateNormal];
    [mChannelButton1 setTitleColor:MAIN_COLOR forState:UIControlStateSelected];
    [mChannelButton1 addTarget:self action:@selector(clickChannelButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mChannelButton1];
    
    [mChannelButton1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navigationBar.bottom).offset(0);
        make.left.equalTo(mChannelButton0.right).offset(0);
        make.size.equalTo(CGSizeMake(MAIN_SCREEN_WIDTH/2, 28));
    }];
    
    //mIndicatorView = UIView.new;
    mIndicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 81, MAIN_SCREEN_WIDTH/2, 1)];
    mIndicatorView.backgroundColor = MAIN_COLOR;
    [self.view addSubview:mIndicatorView];
    
    /*
    //这样写滑动切换侧边栏有bug
    [mIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(mChannelButton0.bottom).offset(0);
        make.left.equalTo(parentView.left).offset(0);
        make.size.equalTo(CGSizeMake(MAIN_SCREEN_WIDTH/2, 1));
    }];
    */
    
    // 瀑布流列表
    mNewsScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 82, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - 82)];
    
    mNewsScrollView.pagingEnabled = YES;  // 控制控件是否整页翻动
    mNewsScrollView.showsHorizontalScrollIndicator = NO;
    mNewsScrollView.showsVerticalScrollIndicator = NO;
    mNewsScrollView.bounces = NO;
    mNewsScrollView.delegate = self;
    [self.view addSubview:mNewsScrollView];

    
    for (int i=0; i<2; i++) {
        
        NewsCollectionView *collectionView = [[NewsCollectionView alloc] initWithFrame:CGRectMake(MAIN_SCREEN_WIDTH * i, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - 82)];
        
        collectionView.newsViewDelegate = self;
        collectionView.channelId = i;
        collectionView.tag = i + NEWS_COLLECTION_VIEW_TAG;
        
        if (i == 0) {
            mCurrentCollectionView = collectionView;
        }
        
        [mNewsScrollView addSubview:collectionView];
    }
    
    mNewsScrollView.contentSize = CGSizeMake(MAIN_SCREEN_WIDTH * 2, MAIN_SCREEN_HEIGHT - 82);
    mNewsScrollView.contentOffset = CGPointZero;
    
    

    
}


// 滚动就触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [mIndicatorView setFrame:CGRectMake((scrollView.contentOffset.x / MAIN_SCREEN_WIDTH) * mIndicatorView.frame.size.width, 81, mIndicatorView.frame.size.width, 1)];
}

// 减速停止了时执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    mScrollViewPage = (int) (scrollView.contentOffset.x/MAIN_SCREEN_WIDTH);
    
    if (mScrollViewPage == 0) {
        mChannelButton0.selected = YES;
        mChannelButton1.selected = NO;
        
    } else if (mScrollViewPage == 1) {
        mChannelButton0.selected = NO;
        mChannelButton1.selected = YES;
    }
    
    [self setCurrentElement];
    
    [self fristLoadData];
    
}

- (void) setCurrentElement {
    
    int tag = mScrollViewPage + NEWS_COLLECTION_VIEW_TAG;
    mCurrentCollectionView = (NewsCollectionView *) [self.view viewWithTag:tag];
}


- (void) clickChannelButton:(UIButton *) button {
    
    if (button.selected == YES) {
        return;
    }
    
    if (button.tag == 0) {
        mChannelButton0.selected = YES;
        mChannelButton1.selected = NO;
        
    } else if (button.tag == 1) {
        mChannelButton0.selected = NO;
        mChannelButton1.selected = YES;
    }
    
    [self setScrollViewSetContentOffset:(int)button.tag];
    
}

- (void) setScrollViewSetContentOffset:(int)currentPage
{
    
    mScrollViewPage = currentPage;
    
    [self setCurrentElement];
    
    [self fristLoadData];
    
    [mNewsScrollView setContentOffset:CGPointMake(MAIN_SCREEN_WIDTH * currentPage, 0) animated:NO];
}





- (void) showMenu {
    
    
    mShadeButton = UIButton.new;
    mShadeButton.backgroundColor = [UIColor blackColor];
    mShadeButton.alpha = 0.0;
    [mShadeButton addTarget:self action:@selector(hiddenMenu) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mShadeButton];
    
    [mShadeButton makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    mRightMenu = [MenuView sharedInstance];
    mRightMenu.menuViewDelegate = self;
    mRightMenu.nav = self.navigationController;
    
    [self.view addSubview:mRightMenu];
    
    
    [UIView animateWithDuration:0.4f animations:^{
        mShadeButton.alpha = MENU_SHADE_ALPHA;
        mRightMenu.frame = CGRectMake(MAIN_SCREEN_WIDTH - MENU_WIDTH, 0, MENU_WIDTH, MAIN_SCREEN_HEIGHT);
        
    }];
    
}

- (void)hiddenMenu {
    [UIView animateWithDuration:0.4f animations:^{
        mRightMenu.frame = CGRectMake(MAIN_SCREEN_WIDTH + MENU_WIDTH, 0, MENU_WIDTH, MAIN_SCREEN_HEIGHT);
        mShadeButton.alpha = 0.0;
    } completion:^(BOOL finished) {
        [mRightMenu removeFromSuperview];
        [mShadeButton removeFromSuperview];
    }];
}

- (void) panToMoveMenu:(float)x {
    if (x > 0) {
        mRightMenu.frame = CGRectMake(MAIN_SCREEN_WIDTH - MENU_WIDTH + x, 0, MENU_WIDTH, MAIN_SCREEN_HEIGHT);
        
        //double a = MENU_WIDTH-x;
        //double alpha = a/(int)MENU_WIDTH * MENU_SHADE_ALPHA;
        //NSLog(@"x=%f alpha=%f %f", a, MENU_WIDTH, a/MENU_WIDTH);
        //mShadeButton.alpha = alpha;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
